package cn.ac.cintcm.synonymous;

import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ImportAccessData {
    
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        
        Connection conn = OracleDb.getConnection();
        if (args != null && args.length == 1) {
            if (args[0].equals("ct")) {
                createEmptyDatabase(conn);
                System.out.println("创建了新的数据库");
            }
        }
        ErrorData.open();
        new ImportAccessData(conn).importDirectory(System.getProperty("user.dir"));
     
        conn.close();
        ErrorData.close();
        System.out.println("导入完成");
    }
    
    
    private static void createEmptyDatabase(Connection conn) throws SQLException {
        NameFieldDao.dropTable(conn, NameFieldDao.CATEGORY);
        NameFieldDao.dropTable(conn, NameFieldDao.SOURCE);
        NameRecordDao.dropTable(conn);
        NameFieldDao.createTable(conn, NameFieldDao.CATEGORY);
        NameFieldDao.createTable(conn, NameFieldDao.SOURCE);
        NameRecordDao.createTable(conn);
    }


    NameFieldDao categoryDao;
    NameFieldDao sourceDao;
    NameRecordDao recordDao;
    MSDb msdb;
    Connection conn;
    
    public ImportAccessData(Connection conn) {
        this.conn = conn;
    }
    
    private void init() {
        categoryDao = new NameFieldDao(conn, NameFieldDao.CATEGORY);
        sourceDao = new NameFieldDao(conn, NameFieldDao.SOURCE);
        recordDao = new NameRecordDao(conn, categoryDao, sourceDao);
    }

    public void importFile(MSDb msdb) throws ClassNotFoundException {
        this.msdb = msdb;
        execute();
    }
    
    public void importDirectory(String path) throws SQLException, ClassNotFoundException {
        File dir = new File(path);
        String[] children = dir.list();
        for (int i=0; i<children.length; i++) {
            // Get filename of file or directory
            String filename = children[i];
            if (filename.endsWith(".mdb")) {
                System.out.println("开始导入" + filename);
                msdb = new MSDb(filename);
                msdb.getConnection();
                execute();
                msdb.close();
            }
        }
    }

    private void execute() throws ClassNotFoundException {
        
        try {
            for(String table : msdb.getTableName()) {
                init();
                ErrorData.setMetaData(table);
                List<NameField> inCategories = msdb.loadCategory(table);
                List<NameField> existCategories = categoryDao.getAll();
                inCategories.removeAll(existCategories);
                categoryDao.saveAll(inCategories);
                List<NameField> inSources = msdb.loadSource(table);
                inSources.removeAll(sourceDao.getAll());
                sourceDao.saveAll(inSources);
                List<String[]> records = msdb.loadData(table);
                List<NameRecord> nameRecords = new ArrayList<NameRecord>();
                for (String[] record : records) {
                    nameRecords.addAll(convertValues(record));
                }
                recordDao.saveAll(nameRecords);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<NameRecord> convertValues(String[] record) {
        List<NameRecord> records = new ArrayList<NameRecord>();
        if (record[3] == null || record[3].trim().equals("")) {
            record[3] = "����";
        }
        NameField category = categoryDao.getNameField(MSDb.trimString(record[3]));
        
        NameField source =  sourceDao.getNameField(MSDb.trimString(record[4]));
        NameRecord formal = new NameRecord();
        formal.setName(record[0]);
        formal.setType(NameRecord.FORMAL);
        formal.setCategory(category);
        formal.setSource(source);
        formal.setReference(record[1]);
        records.add(formal);
        if (record[1] != null) {
            String[] alias = record[1].split("\\$");
            for (int i=0; i < alias.length; i++) {
                if (!alias[i].trim().equals("")) {
                    NameRecord alia = new NameRecord();
                    alia.setName(alias[i]);
                    alia.setType(NameRecord.ALIAS);
                    alia.setCategory(category);
                    alia.setSource(source);
                    alia.setReference(formal.getName());
                    records.add(alia);
                }
            }
        }
        if (record[2] != null && !record[2].trim().equals("")) {
            NameRecord english = new NameRecord();
            english.setName(record[2]);
            english.setType(NameRecord.ENGLISH);
            english.setCategory(category);
            english.setSource(source);
            english.setReference(formal.getName());
            records.add(english);
        }
        return records;
    }
}
